package com.jn.erp.web.api.student.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.jn.erp.utils.DateUtils;
import com.jn.erp.web.api.course.entity.TCourseOrder;
import com.jn.erp.web.api.course.entity.TCourseOrderConfig;
import com.jn.erp.web.api.course.mapper.TCourseOrderConfigMapper;
import com.jn.erp.web.api.course.mapper.TCourseOrderMapper;
import com.jn.erp.web.api.course.task.TCourseOrderTask;
import com.jn.erp.web.api.student.entity.TStudentAppointment;
import com.jn.erp.web.api.student.entity.dto.TStudentAppointmentDto;
import com.jn.erp.web.api.student.mapper.TStudentAppointmentMapper;
import com.jn.erp.web.api.student.service.TStudentAppointmentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;

@Service
public class TStudentAppointmentServiceImpl extends ServiceImpl<TStudentAppointmentMapper, TStudentAppointment> implements TStudentAppointmentService {

    @Autowired
    private TStudentAppointmentMapper tStudentAppointmentMapper;

    @Autowired
   private TCourseOrderMapper tCourseOrderMapper;


    @Autowired
   private TCourseOrderTask tCourseOrderTask;

    @Autowired
    private TCourseOrderConfigMapper TCourseOrderConfigMapper;

    @Override
    public List<TStudentAppointmentDto> auditionList(TStudentAppointmentDto tStudentAppointmentDto) {
        List<TStudentAppointmentDto> list =  tStudentAppointmentMapper.auditionList(tStudentAppointmentDto);
        return list;
    }

    @Override
    public void cancelAudition(TStudentAppointmentDto tStudentAppointmentDto) {
        TCourseOrder tCourseOrder = tCourseOrderMapper.selectById(tStudentAppointmentDto.getId());
        // 如果是试听学员的话取消
        if (tCourseOrder.getTemp() == 3){
            tCourseOrder.setType(6);
        }
        tCourseOrderMapper.updateById(tCourseOrder);
    }

    @Override
    public List<TStudentAppointmentDto> auditionLessonsList(TStudentAppointmentDto tStudentAppointmentDto) {
        return tStudentAppointmentMapper.auditionLessonsList(tStudentAppointmentDto);
    }

    @Override
    public void startClassAudition(TStudentAppointmentDto tStudentAppointmentDto) {

        //可能存在Redis容灾不生效的的情况取数据库数据进行初始化
        tCourseOrderTask.initOrderNo();
       // 先排课
        TCourseOrderConfig tCourseOrderConfig = new TCourseOrderConfig();
        tCourseOrderConfig.setClassId(tStudentAppointmentDto.getClassId());
        tCourseOrderConfig.setCourseId(tStudentAppointmentDto.getCourseId());
        // 上课时间
        tCourseOrderConfig.setClassStartTime(tStudentAppointmentDto.getClassStartTime());
        tCourseOrderConfig.setClassEndTime(tStudentAppointmentDto.getClassEndTime());
        //上课日期
        tCourseOrderConfig.setClassDate(tStudentAppointmentDto.getClassDates());
        tCourseOrderConfig.setTeacher(Long.valueOf(tStudentAppointmentDto.getTeacherId()));
        tCourseOrderConfig.setOrgId(tStudentAppointmentDto.getOrgId());
        tCourseOrderConfig.setClassRoom(tStudentAppointmentDto.getClassRoom());
        tCourseOrderConfig.setIsDelete(String.valueOf(1));
        // 不记录考勤
        tCourseOrderConfig.setExcess(0);
        TCourseOrderConfigMapper.insert(tCourseOrderConfig);
        // 新增课次
        LocalDateTime localDateTime = DateUtils.getLocalDateTime(tStudentAppointmentDto.getClassDates() + " 00:00:00", "yyyy-MM-dd HH:mm:ss");
        String courseOrderNo = tCourseOrderTask.getOrderNo1(localDateTime , "courseOrderNo", tStudentAppointmentDto.getCourseId());
        TCourseOrder dto = new TCourseOrder();
        // 新增开班补课
        String[] stuIds = tStudentAppointmentDto.getStuIds();
        for (int i = 0; i < stuIds.length; i++) {
            dto.setStudentId(Long.valueOf(stuIds[i]));
        }
            dto.setCofId(tCourseOrderConfig.getId());
            dto.setClassId(tStudentAppointmentDto.getClassId());
            dto.setCourseId(tStudentAppointmentDto.getCourseId());
            // 上课时间
            dto.setClassStartTime(tStudentAppointmentDto.getClassStartTime());
            dto.setClassEndTime(tStudentAppointmentDto.getClassEndTime());
            //上课日期
            LocalDateTime time = DateUtils.getLocalDateTime(tStudentAppointmentDto.getClassDates() + " 00:00:00", "yyyy-MM-dd HH:mm:ss");
            dto.setClassDate(time);
            dto.setTeacher(Long.valueOf(tStudentAppointmentDto.getTeacherId()));
            dto.setDescription(tStudentAppointmentDto.getDescription());
            dto.setAssistant(tStudentAppointmentDto.getAssistant());
            dto.setTemp(3);
            dto.setOrderNo(courseOrderNo);
            dto.setOrgId(tStudentAppointmentDto.getOrgId());
            dto.setId(null);
            dto.setIsDelete("0");
            dto.setClassRoom(tStudentAppointmentDto.getClassRoom());
            dto.setType(1);
            dto.setClassHour(0);
            dto.setClassHourPay(0);
            dto.setClassHourPayNormal(0);
            tCourseOrderMapper.insert(dto);
    }



}